import 'package:flutter/services.dart';
import 'package:my_flutter_core/config/general_config.dart';

typedef MethodHandler = Future<dynamic> Function(MethodCall call);

/// This is the service to interact with native platform.
/// Once you changed the code in native you have to restart your Flutter project.
/// NOT HOT RELOAD. NOT HOT RESTART. NONE OF THESE WORKED TO REFRESH THE CODE CHANGE IN NATIVE.
class PlatformChannelService {
  static const MethodChannel _methodChannel = MethodChannel(
    GeneralConfig.methodChannelName,
  );

  /// Invokes a [method] on this channel with the specified [arguments].
  ///
  /// The static type of [arguments] is `dynamic`, but only values supported by
  /// the [codec] of this channel can be used. The same applies to the return
  /// result. The values supported by the default codec and their platform-specific
  /// counterparts are documented with [StandardMessageCodec].
  ///
  /// The generic argument `T` of the method can be inferred by the surrounding
  /// context, or provided explicitly. If it does not match the return type of
  /// the channel, a [TypeError] will be thrown at runtime. `T` cannot be a class
  /// with generics other than `dynamic`. For example, `Map<String, String>`
  /// is not supported but `Map<dynamic, dynamic>` or `Map` is.
  ///
  /// Returns a [Future] which completes to one of the following:
  static Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
    return _methodChannel.invokeMethod<T>(method, arguments);
  }

  static void setMethodCallHandler(MethodHandler methodHandler) {
    _methodChannel.setMethodCallHandler(methodHandler);
  }

  /// Creates an [EventChannel] with [name].
  ///
  /// The example of [name] is 'opennews.openplatform/we_shop.charging_event'.
  static EventChannel createEventChannel(String name) {
    return EventChannel(name);
  }
}
